#include "data_global.h"
#include "sem.h"

#define N 1024

extern int msgid;
extern int shmid;
extern int semid;

extern key_t msg_key;
extern key_t shm_key;
extern key_t sem_key;

// 互斥锁
extern pthread_mutex_t mutex_client_request,
                mutex_refresh,
                mutex_sqlite,
                mutex_transfer,
                mutex_sms,
                mutex_buzzer,
                mutex_led;

// 条件锁
extern pthread_cond_t  cond_client_request,
                cond_refresh,
                cond_sqlite,
                cond_transfer,
                cond_sms,
                cond_buzzer,
                cond_led;

extern struct env_info_client_addr sm_all_env_info;

struct shm_addr
{
    char shm_status;
    struct env_info_client_addr sm_all_env_info;
};
struct shm_addr *shm_buf;

int file_env_info_struct(struct env_info_client_addr *rt_status, int home_id);
int file_env_info_update(struct env_info_client_addr *rt_status, int home_id);

void *pthread_refresh(void *arg)
{
    if((sem_key = ftok("/tmp", 'i')) < 0)
    {
        perror("ftok");
        exit(-1);
    }
    printf("sem_key = %x \n", sem_key);

    semid = semget(sem_key, 1, IPC_CREAT|IPC_EXCL|0666);
    if(semid == -1)
    {
        if(errno == EEXIST)
            semid = semget(sem_key, 1, 0777);
        else
        {
            perror("semget");
            exit(1);
        }
    }
    init_sem(semid, 0, 1);

    if((shm_key = ftok("/tmp", 'i')) < 0)
    {
        perror("ftok");
        exit(-1);
    }
    printf("shm_key = %x \n", shm_key);

    shmid = shmget(shm_key, N, IPC_CREAT|IPC_EXCL|0666);
    if(shmid == -1)
    {
        if(errno == EEXIST)
            shmid = shmget(shm_key, N, 0777);
        else
        {
            perror("shmget");
            exit(1);
        }
    }

    if((shm_buf = (struct shm_addr*)shmat(shmid, NULL, 0)) == (void*)-1)
    {
        perror("shmat");
        exit(1);
    }

    printf("pthread_refresh .......>>>>>>>\n");

#if 1
    int env_info_size = sizeof(struct env_info_client_addr);
    printf("env_info_size = %d\n", env_info_size);
    
	bzero (shm_buf, sizeof (struct shm_addr));
	while(1){
		sem_p(semid, 0);
		shm_buf->shm_status = 1;
		// file_env_info_struct(&shm_buf->sm_all_env_info, shm_buf->shm_status);
		file_env_info_update(&shm_buf->sm_all_env_info, shm_buf->shm_status);
		sem_v(semid,0);
		sleep(1);
	}
#endif 
}

int file_env_info_struct(struct env_info_client_addr *rt_status, int home_id)
{

    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->temperature = 10.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->tempMIN = 2.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->tempMAX = 20.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->humidity = 20.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->humidityMIN = 10.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->humidityMAX = 30.0;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->reserved[0] = 0.01;
    ((struct makeru_zigbee_info*)rt_status->monitor_no[home_id].zigbee_info.buffer)->reserved[1] = -0.01;
    crc16_set((uint8_t*)&rt_status->monitor_no[home_id].zigbee_info, sizeof(rt_status->monitor_no[home_id].zigbee_info));

    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->adc = 9.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->gyrox = -14.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->gyroy = 20.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->gyroz = 40.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->accx = 642.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->accy = -34.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->accz = 5002.0;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->adc = 0.01;
    ((struct makeru_a9_info*)rt_status->monitor_no[home_id].a9_info.buffer)->adc = -0.01;
    crc16_set((uint8_t*)&rt_status->monitor_no[home_id].a9_info, sizeof(rt_status->monitor_no[home_id].a9_info));

    return 0;
}

int file_env_info_update(struct env_info_client_addr *rt_status, int home_id)
{
    pthread_mutex_lock(&mutex_transfer);
    *rt_status = sm_all_env_info;
    pthread_mutex_unlock(&mutex_transfer);
    crc16_set((uint8_t*)&rt_status->monitor_no[home_id].zigbee_info, sizeof(rt_status->monitor_no[home_id].zigbee_info));
    crc16_set((uint8_t*)&rt_status->monitor_no[home_id].a9_info, sizeof(rt_status->monitor_no[home_id].a9_info));

    return 0;
}






